package cj.web.portal.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.constants.CategoryEnum;
import cj.web.admin.constants.IndustryCategoryEnum;
import cj.web.portal.dao.ConfigDAO;
import cj.web.portal.dao.LineDAO;
import cj.web.portal.utils.ConfigConstants;

/**
 * 集团产业
 * @author HanMin
 *
 * 2015年12月29日
 */
@Path("org")
public class OrgController extends BaseController{

	private static Logger logger = Logger.getLogger(OrgController.class.getName());
	@Autowired
	private LineDAO lineDAO;
	@Autowired
	private ConfigDAO configDAO;
	
	//长江快递----------------------Begin
	@Get("jjkd")
	public String showJJKD(Model model)
	{
		model.add("head", "jtcy");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		//快递公司-公司新闻
		List<Map> gsxwList = formIndustry(String.valueOf(IndustryCategoryEnum.KDGS_GSXW.getValue()), 0, 5);
		//快递公司-最新活动
		List<Map> zxhdList = formIndustry(String.valueOf(IndustryCategoryEnum.KDGS_ZXHD.getValue()), 0, 5);
		//快递公司-行业动态
		List<Map> hydtList = formIndustry(String.valueOf(IndustryCategoryEnum.KDGS_HYDT.getValue()), 0, 5);
		
		//快递公司-公司新闻-Pic
		model.add("gsxwList", gsxwList);
		if(gsxwList!=null && gsxwList.size()>0)
		{
			model.add("gsxwListHeardPic", gsxwList.get(0)==null?null:gsxwList.get(0).get("carouselImage").toString());
		}
		else
		{
			model.add("gsxwListHeardPic", null);
		}
		
		//快递公司-最新活动-Pic
		model.add("zxhdList", zxhdList);
		if(zxhdList!=null && zxhdList.size()>0)
		{
			model.add("zxhdListHeardPic", zxhdList.get(0)==null?null:zxhdList.get(0).get("carouselImage").toString());
		}
		else
		{
			model.add("zxhdListHeardPic", null);
		}
		
		//快递公司-行业动态-Pic
		model.add("hydtList", hydtList);
		if(hydtList!=null && hydtList.size()>0)
		{
			model.add("hydtListHeardPic", hydtList.get(0)==null?null:hydtList.get(0).get("carouselImage").toString());
		}
		else
		{
			model.add("hydtListHeardPic", null);
		}
		
		//----------快递公司首页导航滚动
		List<Map> lst = lineDAO.queryIndustryByTitle(Constants.orgKey.kdKey.SYDHGD);	
		
		if (lst !=null && lst.size()>0) {
			model.add("lubbotitle", lst.get(0).get("content"));
		}
		
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		
		return "org/kdgs/jjkd.vm";
	}
	
	//新闻动态-列表
	@Get("shwoJJKDList")
	public String shwoJJKDList(Model model, @Param("category")int category){
		model.add("category",category);
		model.add("categoryName", IndustryCategoryEnum.getName(category).substring(5));
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		return "org/kdgs/list.vm";
	}
	
	//新闻动态-详情
	@Get("shwoJJKDDetail")
	public String shwoJJKDDetail(Model model, @Param("id")String id){
		List<Map> lst = lineDAO.queryIndustryById(id);	
		
		if (lst !=null && lst.size()>0) {
			model.add("id", lst.get(0).get("id"));
			model.add("title", lst.get(0).get("title"));
			model.add("abstractInfor", lst.get(0).get("abstractInfor"));
			model.add("carouselImage", lst.get(0).get("carouselImage"));
			model.add("content", lst.get(0).get("content"));
			model.add("author", lst.get(0).get("author"));
			model.add("source", lst.get(0).get("source"));
			model.add("releaseAT", DateUtils.getDateTime2Str(DateUtils.parse(lst.get(0).get("releaseAT").toString()),
					DateUtils.DATE_FULL_STR));
			model.add("category", lst.get(0).get("category"));
			model.add("categoryName", IndustryCategoryEnum.getName(Integer.parseInt(lst.get(0).get("category").toString())).substring(5));
		}
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		return "org/kdgs/detail.vm";
	}
	
	//关于我们
	@Get("gywm")
	public String showGYWM(Model model)
	{
		model = returnKDModel("gywm", "jtcy", Constants.orgKey.kdKey.GYWM, model);
		return "org/kdgs/gywm.vm";
	}
	
	//招贤纳士
	@Get("zxns")
	public String showZXNS(Model model)
	{
		model = returnKDModel("zxns", "jtcy", Constants.orgKey.kdKey.ZXNS, model);
		return "org/kdgs/zxns.vm";
	}
	
	//联系我们
	@Get("lxwm")
	public String showLXWM(Model model)
	{
		model = returnKDModel("lxwm", "jtcy", Constants.orgKey.kdKey.LXWM, model);
		return "org/kdgs/lxwm.vm";
	}
	
	//公司业务-广告分公司
	@Get("ggfgs")
	public String showGGFGS(Model model)
	{
		model = returnKDModel("ggfgs", "jtcy", Constants.orgKey.kdKey.GGFGS, model);
		return "org/kdgs/ggfgs.vm";
	}
	
	//公司业务-商务分公司
	@Get("swfgs")
	public String showSWFGS(Model model)
	{
		model = returnKDModel("swfgs", "jtcy", Constants.orgKey.kdKey.SWFGS, model);
		return "org/kdgs/swfgs.vm";
	}
	
	//公司业务-速递分公司
	@Get("sdfgs")
	public String showSDFGS(Model model)
	{
		model = returnKDModel("sdfgs", "jtcy", Constants.orgKey.kdKey.SDFGS, model);
		return "org/kdgs/sdfgs.vm";
	}
	
	//公司业务-物流分公司
	@Get("wlfgs")
	public String showWLFGS(Model model)
	{		
		model = returnKDModel("wlfgs", "jtcy", Constants.orgKey.kdKey.WLFGS, model);
		return "org/kdgs/wlfgs.vm";
	}
	
	/**
	 * 返回快递Model
	 * @param memu		菜单名称
	 * @param head		头部名称
	 * @param kdKey		快递Key
	 * @param model
	 * @return
	 */
	private Model returnKDModel(String memu,String head,String kdKey,Model model)
	{
		model.add("head", head);
		model.add("memu", memu);
		List<Map> lst = lineDAO.queryIndustryByTitle(kdKey);	
		
		if (lst !=null && lst.size()>0) {
			model.add("id", lst.get(0).get("id"));
			model.add("title", lst.get(0).get("title"));
			model.add("abstractInfor", lst.get(0).get("abstractInfor"));
			model.add("carouselImage", lst.get(0).get("carouselImage"));
			model.add("content", lst.get(0).get("content"));
			model.add("author", lst.get(0).get("author"));
			model.add("source", lst.get(0).get("source"));
			model.add("releaseAT", DateUtils.getDateTime2Str(DateUtils.parse(lst.get(0).get("releaseAT").toString()),
					DateUtils.DATE_FULL_STR));
			model.add("category", lst.get(0).get("category"));
			model.add("categoryName", IndustryCategoryEnum.getName(Integer.parseInt(lst.get(0).get("category").toString())).substring(5));
		}
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.KD_LB);
		
		model.add("pics", pics);
		return model;
	}
	
	//长江快递----------------------End
	
	//长江网
	@Get("jjw")
	public String showJJW(Model model)
	{
		model.add("head", "jtcy");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/jjw.vm";
	}

	//网点公司
	@Get("wdgs")
	public String showWDGS(Model model)
	{
		model.add("head", "jtcy");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/wdgs.vm";
	}
	
	//发行公司
	@Get("fxgs")
	public String showFXGS(Model model)
	{
		model.add("head", "jtcy");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/fxgs.vm";
	}
	
	//印务公司----------------------Begin
	@Get("ywgs")
	public String showYWGS(Model model)
	{
		//首页LB
		List<Map> pics = configDAO.queryByCode(ConfigConstants.YW_LB);
		
		model.add("pics", pics);
		model.add("head", "jtcy");
		
		//印务公司动态
		List<Map> objs = formIndustry(String.valueOf(IndustryCategoryEnum.YWGS_GSXW.getValue()),0,8);
		
		//印务公司动态top
		List<Map> queryByImages = queryByImageIsNotNull(String.valueOf(IndustryCategoryEnum.YWGS_GSXW.getValue()));
		
		if (queryByImages != null && queryByImages.size() > 0) {
			model.add("top", queryByImages.get(0));
		}
		else
		{
			model.add("top", objs.size()>0?objs.get(0):null);
		}
		
		model.add("cjrbpaperls", objs);//长江日报最新动态
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/ywgs.vm";
	}
	
	//印务公司简介
	@Get("ywgsjj")
	public String showYWGSJJ(Model model)
	{
		model.add("memu", "ywgsjj");
		model.add("head", "jtcy");
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));
		return "org/ywgs/ywgsjj.vm";
	}
	
	@Get("more")
	public String more(Model model, @Param("category")int category){		
		model.add("category",category);
		model.add("categoryName", IndustryCategoryEnum.getName(category).substring(5));
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/ywgs/list.vm";
	}
	
	@Get("newsList")    //长江金服 新闻列表
	public String newsList(Model model, HttpServletRequest request) throws Exception{
		Map<String, String> config = getConfigSign(request);
		model.add("Config",config);
		
		model.add("Title", "长江金服");
		model.add("Desc", "只为您专属打造");
		return "cjjf/newsList.vm";
	}
	
	@Get("newsDetail") //长江金服 新闻详情
	public String newsDetail(Model model, @Param("id")String id, HttpServletRequest request) throws Exception{
		List<Map> lst = lineDAO.queryIndustryById(id);
		
		if (lst !=null && lst.size()>0) {
			Map obj = lst.get(0);
			obj.put("releaseAT", DateUtils.formatDateStr(obj.get("releaseAT").toString(),DateUtils.DATE_FULL_STR));
			model.add("Obj", obj);
		}
		Map<String, String> config = getConfigSign(request);
		model.add("Config",config);
		
		model.add("Title", "长江金服");
		model.add("Desc", "只为您专属打造");
		return "cjjf/newsDetail.vm";
	}
	
	@Get("lstJf")  //长江金服 新闻数据接口
	public String lstJf(Model model, @Param("start") int start, @Param("length") int length){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String category = String.valueOf(IndustryCategoryEnum.CJJF_XW.getValue());
		List<Map> list = lineDAO.queryIndustry(category, start*length, length);
		for( Map map : list ){
			Object objImg = map.get("carouselImage");
			if( objImg != null && !"".equals(objImg.toString()) ){
				String[] path = objImg.toString().split("/");
				map.put("carouselImage", path[0] + "/s_" + path[1]);
			}
		}
		jo.put("list",list);
		
		return "@" + jo.toString();
	}
	
	@Get("lst")
	public String lst(Model model, @Param("category")String category, @Param("start") int start,
			@Param("length") int length){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> list = lineDAO.queryIndustry(category, start*length, length);
		jo.put("list",list);
		
		return "@" + jo.toString();
	}
	
	@Get("detail")
	public String detail(Model model, @Param("id")String id){
		List<Map> lst = lineDAO.queryIndustryById(id);	
		
		if (lst !=null && lst.size()>0) {
			model.add("id", lst.get(0).get("id"));
			model.add("title", lst.get(0).get("title"));
			model.add("abstractInfor", lst.get(0).get("abstractInfor"));
			model.add("carouselImage", lst.get(0).get("carouselImage"));
			model.add("content", lst.get(0).get("content"));
			model.add("author", lst.get(0).get("author"));
			model.add("source", lst.get(0).get("source"));
			model.add("releaseAT", DateUtils.getDateTime2Str(DateUtils.parse(lst.get(0).get("releaseAT").toString()),
					DateUtils.DATE_FULL_STR));
			model.add("category", lst.get(0).get("category"));
			model.add("categoryName", IndustryCategoryEnum.getName(Integer.parseInt(lst.get(0).get("category").toString())).substring(5));
		}
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/ywgs/detail.vm";
	}
	
	@Get("ppcyDetail")
	public String ppcyDetail(Model model, @Param("id")String id){
		List<Map> lst = lineDAO.queryIndustryById(id);	
		
		if (lst !=null && lst.size()>0) {
			model.add("id", lst.get(0).get("id"));
			model.add("title", lst.get(0).get("title"));
			model.add("abstractInfor", lst.get(0).get("abstractInfor"));
			model.add("carouselImage", lst.get(0).get("carouselImage"));
			model.add("content", lst.get(0).get("content"));
			model.add("author", lst.get(0).get("author"));
			model.add("source", lst.get(0).get("source"));
			model.add("releaseAT", DateUtils.getDateTime2Str(DateUtils.parse(lst.get(0).get("releaseAT").toString()),
					DateUtils.DATE_FULL_STR));
			model.add("category", lst.get(0).get("category"));
			model.add("categoryName", IndustryCategoryEnum.getName(Integer.parseInt(lst.get(0).get("category").toString())).substring(5));
		}
		model.add("memu",id);
		model.add("jtpphdls", jtpphd(String.valueOf(CategoryEnum.CJTT_PPHD.getValue())));//集团品牌活动列表
		return "org/ywgs/ppcyDetail.vm";
	}
	//印务公司----------------------End
	
	private List<Map> queryByImageIsNotNull(String category) {
		List<Map> ls = lineDAO.queryIndustryByImageIsNotNull(category, 0, 1);
		List<Map> resls = new ArrayList<Map>();
		for(Map rep :ls)
		{
			Map l_rep = new HashMap();
			l_rep.put("id", rep.get("id"));//ID
			String title=rep.get("title").toString();
			if(title.length()>20)
			{
				title=title.toString().substring(0, 20)+"...";
			}
			l_rep.put("title", title);//标题
			l_rep.put("abstractInfor", rep.get("abstractInfor"));//简介
			//缩率图
			String carouselImage = rep.get("carouselImage")==null?"":rep.get("carouselImage").toString();
//			if(carouselImage !=null && !"".equals(carouselImage))
//			{
//				String carouselImages[] = carouselImage.split("/");
//				l_rep.put("carouselImage", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
//			}
//			else
//			{
//				l_rep.put("carouselImage", "");//缩率图
//			}
			l_rep.put("carouselImage", carouselImage);//缩率图
			l_rep.put("releaseAT", DateUtils.getMinusTime(rep.get("releaseAT").toString(),DateUtils.DATE_SMALL_STR,0));//发布时间
			resls.add(l_rep);
		}
		return resls;
	}
	

	private List<Map> formIndustry(String category, int start, int length) {
		List<Map> ls = lineDAO.queryIndustry(category, start, length);
		List<Map> resls = new ArrayList<Map>();
		for(Map rep :ls)
		{
			Map l_rep = new HashMap();
			l_rep.put("id", rep.get("id"));//ID
			String title=rep.get("title").toString();
			if(title.length()>20)
			{
				title=title.toString().substring(0, 20)+"...";
			}
			l_rep.put("title", title);//标题
			l_rep.put("abstractInfor", rep.get("abstractInfor"));//简介
			//缩率图
			String carouselImage = rep.get("carouselImage")==null?"":rep.get("carouselImage").toString();
//			if(carouselImage !=null && !"".equals(carouselImage))
//			{
//				String carouselImages[] = carouselImage.split("/");
//				l_rep.put("carouselImage", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
//			}
//			else
//			{
//				l_rep.put("carouselImage", "");//缩率图
//			}
			l_rep.put("carouselImage", carouselImage);//缩率图
			l_rep.put("releaseAT", DateUtils.getMinusTime(rep.get("releaseAT").toString(),DateUtils.DATE_SMALL_STR,0));//发布时间
			resls.add(l_rep);
		}
		return resls;
	}
	
	/**
	 * 集团down
	 * @param category
	 * @return
	 */
	private List<Map> jtpphd(String category)
	{
		//集团品牌活动
		List<Map> ppls = lineDAO.query(category, 0, 5);
		List<Map> pphdls = new ArrayList<Map>();
		for(Map rep :ppls)
		{
			Map l_rep = new HashMap();
			l_rep.put("id", rep.get("id"));//ID
			String title=rep.get("title").toString();
			if(title.length()>15)
			{
				title=title.toString().substring(0, 15)+"...";
			}
			l_rep.put("title", title);//标题
			l_rep.put("abstractInfor", rep.get("abstractInfor"));//简介
			//缩率图
			String carouselImage = rep.get("carouselImage")==null?"":rep.get("carouselImage").toString();
//			if(carouselImage !=null && !"".equals(carouselImage))
//			{
//				String carouselImages[] = carouselImage.split("/");
//				l_rep.put("carouselImage", carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
//			}
//			else
//			{
//				l_rep.put("carouselImage", "");//缩率图
//			}
			l_rep.put("carouselImage", carouselImage);//缩率图
			pphdls.add(l_rep);
		}
		return pphdls;
	}
}
